rm(list=ls()) #clear

library(summarytools)
library(expss)
library(dplyr)
library(psych)

std01<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
} 
#recode vars 0 to 1

dat<- read.csv("Study 4 - Bovitz/Wave 3/Bovitz_Qual_S4W3.csv")
load("Study 4 - Bovitz/Wave 2/demoexport.RData")

dat<-merge(dat, demoexport, by="PID", all = "TRUE")

###removing disqualified and Rs from other group###

dat<- subset(dat, dat$PID!="")
dat<- subset(dat, dat$Finished==1)
dat<- subset(dat, dat$Consent==1)
dat<- subset(dat, dat$atnchk1=="1,2")
dat<- dat[!(duplicated(dat$PID) | duplicated(dat$PID, fromLast = TRUE)), ]

dat$pid7<- ifelse(dat$pidcat==1 & dat$demstr==1, 1,
                  ifelse(dat$pidcat==1 & dat$demstr==2, 2,
                         ifelse(dat$pidcat==2 & dat$demstr==1, 7,
                                ifelse(dat$pidcat==2 & dat$demstr==2, 6, dat$lean))))

################################### DEMOGRAPHIC VARS ##################################
dat$white<- ifelse(dat$ethnicity==1, 1, 0)
dat$black<- ifelse(dat$ethnicity==2, 1, 0)
dat$asian<- ifelse(dat$ethnicity==3, 1, 0)
dat$male<- ifelse(dat$gender==1, 1, 0)
dat$hisplat<- ifelse(dat$hispanic==1, 1, 0)
dat$inc<- dat$income
dat$ba<- ifelse(dat$education > 5, 1, 0)

freq(dat$white)
freq(dat$black)
freq(dat$asian)
freq(dat$male)
freq(dat$hisplat)
freq(dat$ba)
mean(dat$age, na.rm = T)

dat<- subset(dat, dat$pid7 == 4)
#only pure independents

dat<- subset(dat, dat$atnchk2==5)
#2nd attention check that was halfway through the survey

################################### normal vars #######################################

#aggregate ps and subscale items for both 20 and 9 versions
dat$otot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8)))
dat$atot20<-rowMeans(with(dat, cbind(a1, a2, a3, a4, a5, a6)))
dat$mtot20<-rowMeans(with(dat, cbind(m1, m2, m3, m4, m5, m6)))
dat$apstot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8,
                                                a1, a2, a3, a4, a5, a6,
                                                m1, m2, m3, m4, m5, m6)))

dat$otot9<-rowMeans(with(dat, cbind(o1, o6, o8)))
dat$atot9<-rowMeans(with(dat, cbind(a1, a2, a4)))
dat$mtot9<-rowMeans(with(dat, cbind(m2, m4, m6)))
dat$apstot9<-rowMeans(with(dat, cbind(o1, o6, o8,
                                               a1, a2, a4, 
                                               m2, m4, m6)))

#feeling therms
dat$ftdem<- dat$ft_2
dat$ftrep<- dat$ft_1
dat$ftparties<- rowMeans(with(dat, cbind(ftdem, ftrep)))

#Huddy et. al. partisan social identity (higher means stronger independent identity)
dat$idtot<-rowMeans(with(dat, cbind(hid1, hid2, hid3, hid4)))

#political knowledge (higher means more political knowledge)
dat$pk1<- ifelse(dat$pk1==4, 1, 0)
dat$pk2<- ifelse(dat$pk2==2, 1, 0)
dat$pk3<- ifelse(dat$pk3==1, 1, 0)
dat$pk4<- ifelse(dat$pk4==3, 1, 0)

dat$pktot<-rowMeans(with(dat, cbind(pk1, pk2, pk3, pk4)))

#partisan violence (higher means more violence)
dat$pv2<- dplyr::recode(dat$pv2, "1"=5, "2"=4, "3"=3, "4"=2, "5"=1)
dat$pvtot<-rowMeans(with(dat, cbind(pv1, pv2, pv3, pv4, pv6, pv7)))

#recode all measures to 0-1
dat<- dat %>%
  mutate_at(c("otot20", "atot20", "mtot20", "apstot20",
              "otot9", "atot9", "mtot9", "apstot9",
              "ftdem", "ftrep", "ftparties",
              "idtot", "pktot", "pvtot"),
            funs(std01(.)))

save(dat, file = "Study 4 - Bovitz/Wave 3/IndDatS4W3.RData")
save(dat, file = "Main Analyses/IndDatS4W3.RData")

